package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.StoreDTO;
import com.carkeeper.entity.BillPO;
import com.carkeeper.entity.OrderPO;
import com.carkeeper.mapper.BillMapper;
import com.carkeeper.service.BillService;
import com.carkeeper.service.OrderService;
import com.carkeeper.service.StoreFeign;
import com.carkeeper.vo.BillVO;
import com.carkeeper.vo.Pager;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class BillServiceImpl implements BillService {
    @Autowired
    private BillMapper billMapper;
    @Autowired
    private OrderService orderService;
    @Autowired
    private StoreFeign storeFeign;

    /* 新增账单
     * */
    @Override
    public void addBill(BillPO billPO) {
        billMapper.insert(billPO);
    }

    /*
     * 获取账单详情
     * */
    @Override
    public BillPO getBill(Long id) {
        OrderPO orderPO = new OrderPO();
        BillPO billPO = billMapper.selectById(id);
        if (billPO != null) {
            orderPO.setId(billPO.getOrderId());
            billPO.setOrderPO(orderPO);
            StoreDTO storeDTO = storeFeign.queryStoreById(billPO.getStoreId());
            billPO.setStoreDTO(storeDTO);
        }
        return billPO;
    }

    /*
     * 删除账单
     * */
    @Override
    public void delById(Long id) {
        billMapper.deleteById(id);
    }
    /*
     * 获取账单分页
     * */

    @Override
    public Page<BillPO> getBillPage(Integer page, Integer pagesize, Integer type, String code,
                                    String storeName, Integer payState, String createTimeStart,
                                    String createTimeEnd, Long storeId) {
        Page<BillPO> poPage = new Page<>(page, pagesize);
        LambdaQueryWrapper<BillPO> wrapper = new LambdaQueryWrapper<>();
        if (type != null) {
            wrapper.eq(BillPO::getType, type);
        }
        if (!StringUtils.isEmpty(code)) {
            wrapper.like(BillPO::getCode, code);
        }
        if (!StringUtils.isEmpty(storeName)) {
            wrapper.like(BillPO::getStoreName, storeName);
        }
        if (payState != null) {
            wrapper.eq(BillPO::getPayState, payState);
        }
        if (!StringUtils.isEmpty(createTimeStart) && !StringUtils.isEmpty(createTimeEnd)) {
            wrapper.ge(BillPO::getCreateTime, createTimeStart).le(BillPO::getCreateTime, createTimeEnd);
        }
        if (storeId != null) {
            wrapper.eq(BillPO::getStoreId, storeId);
        }
        Page<BillPO> billPOPage = billMapper.selectPage(poPage, wrapper);
        return billPOPage;
    }

    /*
     * 根据结算单id获取账单
     * */
    @Override
    public BillPO getByStoreId(Long storeId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("store_id", storeId);
        List<BillPO> billPOS = billMapper.selectByMap(map);
        return billPOS.get(0);
    }

    @Override
    public List<BillPO> findAll(Long storeId, Integer payState) {
        LambdaQueryWrapper<BillPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (storeId != null) {
            lambdaQueryWrapper.eq(BillPO::getStoreId, storeId);
        }
        if (payState != null) {
            lambdaQueryWrapper.eq(BillPO::getPayState, payState);
        }
        return billMapper.selectList(lambdaQueryWrapper);
    }
}
